<?php

namespace App\Console\Commands\User;

use Illuminate\Console\Command;
use App\Models\User;

class DumpUserCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'user:dump';

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:dump
        {username : The username}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Dump the details of a user';

    /**
     * 要打印显示的字段
     */
    protected $fields = [
        'username',
        'password',
        'fullname',
        'lucky',
        'role',
        'is_alive',
        'register_at',
        'last_login_at',
    ];

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        // 获取参数
        $username = $this->argument('username');

        // 查找用户
        $user = User::where('username', $username)->first();
        if ($user) {
            $this->info('User found!');
            foreach ($this->fields as $key) {
                $this->line($key . ' = ' . $user->{$key});
            }
        } else {
            $this->error('User not found!');
        }
    }
}
